import 'dk_refresh_accessory.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter/gestures.dart';

class DkRefreshListWidget extends StatelessWidget {
  /// 控制器
  final EasyRefreshController controller;

  /// 刷新回调(null为不开启刷新)
  final OnRefreshCallback onRefresh;

  /// 加载回调(null为不开启加载)
  final OnLoadCallback onLoad;

  /// 是否开启控制结束刷新
  final bool enableControlFinishRefresh;

  /// 是否开启控制结束加载
  final bool enableControlFinishLoad;

  /// 任务独立(刷新和加载状态独立)
  final bool taskIndependence;

  /// Header
  final Header header;
  final int headerIndex;

  /// Footer
  final Footer footer;

  /// 子组件构造器
  final EasyRefreshChildBuilder builder;

  /// 子组件
  final Widget child;

  /// 首次刷新
  final bool firstRefresh;

  /// 首次刷新组件
  /// 不设置时使用header
  final Widget firstRefreshWidget;

  /// 空视图
  /// 当不为null时,只会显示空视图
  /// 保留[headerIndex]以上的内容
  final emptyWidget;

  /// 顶部回弹(onRefresh为null时生效)
  final bool topBouncing;

  /// 底部回弹(onLoad为null时生效)
  final bool bottomBouncing;

  /// Slivers集合
  final List<Widget> slivers;

  /// 列表方向
  final Axis scrollDirection;

  /// 反向
  final bool reverse;
  final ScrollController scrollController;
  final bool primary;
  final bool shrinkWrap;
  final Key center;
  final double anchor;
  final double cacheExtent;
  final int semanticChildCount;
  final DragStartBehavior dragStartBehavior;

  /// 全局默认Header
  static DkClassicalHeader _defaultHeader = DkClassicalHeader(
      refreshText: "下拉刷新页面",
      refreshReadyText: "松开刷新",
      refreshingText: "刷新中...",
      refreshedText:"刷新完成",
      refreshFailedText: "刷新失败",
      noMoreText:"没有更多数据了",
      infoText: "上次更新 %T"
  );
  static set defaultHeader(DkClassicalHeader header) {
    if (header != null) {
      _defaultHeader = header;
    }
  }
  /// 全局默认Footer
  static DkClassicalFooter _defaultFooter = DkClassicalFooter(
    loadText: "上拉加载更多数据",
    loadReadyText: "松开加载",
    loadingText: "加载中...",
    loadedText: "加载完成",
    loadFailedText: "加载失败",
    noMoreText: "没有更多数据了",
    infoText: "上次更新 %T",
    bgColor: Colors.transparent,
    textColor: Colors.black87,
    infoColor: Colors.black54,
  );
  static set defaultFooter(DkClassicalFooter footer) {
    if (footer != null) {
      _defaultFooter = footer;
    }
  }

  /// custom构造器(推荐)
  /// 直接使用CustomScrollView可用的slivers
  DkRefreshListWidget.custom({
    key,
    this.controller,
    this.onRefresh,
    this.onLoad,
    this.enableControlFinishRefresh = true,
    this.enableControlFinishLoad = true,
    this.taskIndependence = false,
    this.header,
    this.headerIndex,
    this.footer,
    this.scrollDirection = Axis.vertical,
    this.reverse = false,
    this.scrollController,
    this.primary,
    this.shrinkWrap = false,
    this.center,
    this.anchor = 0.0,
    this.cacheExtent,
    this.semanticChildCount,
    this.dragStartBehavior = DragStartBehavior.start,
    this.firstRefresh,
    this.firstRefreshWidget,
    this.emptyWidget,
    this.topBouncing = true,
    this.bottomBouncing = true,
    @required this.slivers,
  })  : this.builder = null,
        this.child = null;

  @override
  Widget build(BuildContext context) {
    return EasyRefresh.custom(
      key : key,
      controller : controller,
      onRefresh : onRefresh,
      onLoad : onLoad,
      enableControlFinishRefresh : enableControlFinishRefresh,
      enableControlFinishLoad : enableControlFinishLoad,
      taskIndependence : taskIndependence,
      header : header ?? DkRefreshListWidget._defaultHeader,
      headerIndex : headerIndex,
      footer : footer ?? DkRefreshListWidget._defaultFooter,
      scrollDirection : scrollDirection,
      reverse : reverse,
      scrollController : scrollController,
      primary : primary,
      shrinkWrap : shrinkWrap,
      center : center,
      anchor : anchor,
      cacheExtent : cacheExtent,
      semanticChildCount : semanticChildCount,
      dragStartBehavior : dragStartBehavior,
      firstRefresh : firstRefresh,
      firstRefreshWidget : firstRefreshWidget,
      emptyWidget : emptyWidget,
      topBouncing : topBouncing,
      bottomBouncing : bottomBouncing,
      slivers : slivers,
    );
  }
}


